package top.lcm889.juc.count_down_latch;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * @author Coder-lcm
 * @date 2021/4/30
 *
 * 模拟顾客-服务员场景：服务员线程需要等顾客线程都到齐才能上菜
 */
public class CustomerThread implements Runnable{
    private CountDownLatch latch;
    private String name;

    public CustomerThread(CountDownLatch latch, String name){
        this.latch = latch;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            System.out.println(LocalDateTime.now() + " " + name + " 出发去饭店");
            Thread.sleep(new Random().nextInt(3) * 3000 + 1000);
            System.out.println(LocalDateTime.now() + " " + name + " 到达饭店");
            latch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
